package org.base.sys.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.base.sys.pojo.SysRoleMenuPermission;
import org.base.sys.dao.SysRoleMenuPermissionMapper;
import org.base.sys.service.SysRoleMenuPermissionService;
import org.base.common.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.base.sys.query.SysRoleMenuPermissionQuery;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色和菜单权限关联 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2020-05-31
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysRoleMenuPermissionServiceImpl extends ServiceImpl<SysRoleMenuPermissionMapper, SysRoleMenuPermission, SysRoleMenuPermissionQuery>implements SysRoleMenuPermissionService {

    @Override
    public List<Long> getRoleMenuPermissionIds(Long roleId) {
        return lambdaQuery().eq(SysRoleMenuPermission::getRoleId, roleId)
                .list()
                .stream()
                .map(SysRoleMenuPermission::getMenuPermissionId)
                .collect(Collectors.toList());
    }

    @Override
    public void updateRoleMenuPermissions(Long roleId, List<Long> menuPermissionIds) {
        lambdaUpdate().eq(SysRoleMenuPermission::getRoleId, roleId)
                .remove();
        if (CollectionUtils.isNotEmpty(menuPermissionIds)) {
            List<SysRoleMenuPermission> data = new ArrayList<>();
            SysRoleMenuPermission roleMenuPermission;
            for (Long menuPermissionId : menuPermissionIds) {
                roleMenuPermission = new SysRoleMenuPermission();
                roleMenuPermission.setRoleId(roleId);
                roleMenuPermission.setMenuPermissionId(menuPermissionId);
                data.add(roleMenuPermission);
            }
            saveBatch(data);
        }
    }

    @Override
    public List<Long> getRolesMenuPermissionIds(List<Long> roleIds) {
        return lambdaQuery().in(SysRoleMenuPermission::getRoleId, roleIds)
                .list()
                .stream()
                .map(SysRoleMenuPermission::getMenuPermissionId)
                .collect(Collectors.toList());
    }

}
